﻿Imports System.IO
Imports System.Environment
Imports System.Text

Public Class ujadatlap

    Function GenerateCode()
        Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        Dim r As New Random
        Dim sb As New StringBuilder
        For i As Integer = 1 To 8
            Dim idx As Integer = r.Next(0, 35)
            sb.Append(s.Substring(idx, 1))
        Next
        sb.Insert(8, Date.Today.Millisecond)
        Return sb.ToString()
    End Function

    Private Sub Bmegse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bmegse.Click
        Close()
    End Sub

    Public x As Integer = 0
    Public kod As String = ""
    Public igaz1 As Boolean = False
    Public igaz2 As Boolean = False

    Private Sub ujadatlap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim f As New StreamReader("dolgozo.txt")

        While Not f.EndOfStream
            Dim s As String
            s = f.ReadLine
            CBszerelo.Items.Add(s)
            CBtszerelo.Items.Add(s)
            f.ReadLine()
        End While
        f.Close()

        CBtetel.Items.Add("Munkafolyamat")
        CBtetel.Items.Add("Anyag")
        CBtetel.Items.Add("Alkatrész")
        CBtetel.SelectedIndex = 0
        CBtszerelo.SelectedIndex = 0
        CBszerelo.SelectedIndex = 0

        TBdatum.Text = Date.Today

        Dim f2 As New StreamReader("adatlapnev.txt")
        Dim f3 As New StreamReader("adatlapszerelo.txt")
        Dim f4 As New StreamReader("adatlapfelvetel.txt")

        RTBsegednev.Text = f2.ReadToEnd
        RTBsegedszerelo.Text = f3.ReadToEnd
        RTBsegedfelvetel.Text = f4.ReadToEnd
        f2.Close()
        f3.Close()
        f4.Close()


    End Sub

    Private Sub Bhozzaad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bhozzaad.Click

        If igaz1 And igaz2 Then
            RTBsegedfelvetel.Text += x.ToString + NewLine + RTBtetelseged.Text + "0" + NewLine
            RTBsegednev.Text += x.ToString + NewLine + RTBtetelseged.Text + "0" + NewLine
            RTBsegedszerelo.Text += x.ToString + NewLine + RTBtetelseged.Text + "0" + NewLine

            Dim f2 As New StreamWriter("adatlapnev.txt")
            Dim f3 As New StreamWriter("adatlapszerelo.txt")
            Dim f4 As New StreamWriter("adatlapfelvetel.txt")

            f2.Write(RTBsegednev.Text)
            f3.Write(RTBsegedszerelo.Text)
            f4.Write(RTBsegedfelvetel.Text)

            f2.Close()
            f3.Close()
            f4.Close()

            MsgBox("Az egyéni kód: " + kod, MsgBoxStyle.Information, "Kód")

            Close()
        Else
            If Not igaz1 Then
                MsgBox("Adatokat töltse ki helyesen!", MsgBoxStyle.Exclamation, "Hiba")
            ElseIf Not igaz2 Then
                MsgBox("Legalább egy tételt adjon hozzá!", MsgBoxStyle.Exclamation, "Hiba")
            Else
                MsgBox("Nem várt hiba.", MsgBoxStyle.Information, "Hiba")
                Close()
            End If
        End If


    End Sub

    Private Sub Btetel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btetel.Click
        If TB1.Text = "" Or TB3.Text = "" Then
            If CBtetel.SelectedIndex = 0 And CBtszerelo.SelectedItem = "" Then
                MsgBox("Ne hagyj üresen egy mezőt sem!", MsgBoxStyle.Exclamation, "Hiba")
            ElseIf TB2.Text = "" Then
                MsgBox("Ne hagyj üresen egy mezőt sem!", MsgBoxStyle.Exclamation, "Hiba")
            End If
        Else

            If CBtetel.SelectedIndex = 0 Then
                RTBtetelseged.Text += CBtetel.SelectedIndex.ToString + NewLine + TB1.Text + NewLine + CBtszerelo.SelectedIndex.ToString + NewLine + TB3.Text + NewLine
            Else
                RTBtetelseged.Text += CBtetel.SelectedIndex.ToString + NewLine + TB1.Text + NewLine + TB2.Text + NewLine + TB3.Text + NewLine
            End If

            MsgBox("Tétel sikeresen hozzáadva.", MsgBoxStyle.Information, "Siker")
            TB1.Text = ""
            TB2.Text = ""
            TB3.Text = ""
            CBtszerelo.SelectedIndex = 0
            x += 1
            igaz2 = True
        End If
    End Sub

    Private Sub Badat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Badat.Click

        If TBlakcim.Text = "" Or TBnev.Text = "" Or TBrendszam.Text = "" Or TBtelefon.Text = "" Or TBtipus.Text = "" Or CBszerelo.SelectedItem = "" Then
            MsgBox("Ne hagyj üresen egy mezőt sem!", MsgBoxStyle.Exclamation, "Hiba")
        Else
            kod = GenerateCode()
            RTBsegednev.Text += TBnev.Text + " [" + kod + "]" + NewLine + kod + NewLine + CBszerelo.SelectedItem.ToString + NewLine + TBdatum.Text + NewLine + TBlakcim.Text + NewLine + TBtelefon.Text + NewLine + TBrendszam.Text + NewLine + TBtipus.Text + NewLine
            RTBsegedfelvetel.Text += TBdatum.Text + NewLine + CBszerelo.SelectedItem.ToString + NewLine + TBnev.Text + " [" + kod + "]" + NewLine + kod + NewLine + TBlakcim.Text + NewLine + TBtelefon.Text + NewLine + TBrendszam.Text + NewLine + TBtipus.Text + NewLine
            RTBsegedszerelo.Text += CBszerelo.SelectedItem.ToString + NewLine + TBdatum.Text + NewLine + TBnev.Text + " [" + kod + "]" + NewLine + kod + NewLine + TBlakcim.Text + NewLine + TBtelefon.Text + NewLine + TBrendszam.Text + NewLine + TBtipus.Text + NewLine
            MsgBox("Sikeresen mentve!", MsgBoxStyle.Information, "Lajos")
            Badat.Visible = False
            Btetel.Enabled = True
            igaz1 = True
        End If



    End Sub

    Private Sub CBtetel_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBtetel.SelectedIndexChanged
        If CBtetel.SelectedIndex = 0 Then
            L1.Text = "Idő"
            L2.Text = "Szerelő"
            L3.Text = "Óradj"
            TB1.Text = ""
            TB2.Text = ""
            TB3.Text = ""
            CBtszerelo.Visible = True
            TB2.Visible = False
        ElseIf CBtetel.SelectedIndex = 1 Then
            L1.Text = "Név"
            L2.Text = "Mennyiség"
            L3.Text = "Egységár"
            TB1.Text = ""
            TB2.Text = ""
            TB3.Text = ""
            CBtszerelo.Visible = False
            TB2.Visible = True
        ElseIf CBtetel.SelectedIndex = 2 Then
            L1.Text = "Név"
            L2.Text = "Cikkszám"
            L3.Text = "Ár"
            TB1.Text = ""
            TB2.Text = ""
            TB3.Text = ""
            CBtszerelo.Visible = False
            TB2.Visible = True
        End If
    End Sub
End Class